﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyHospital.DAL;

namespace MyHospital.BLL
{

    public class Appraise1
    {
        MyHospitalDataContext db = new MyHospitalDataContext();
        int[] id = new int[5] { 0, 0, 0, 0, 0 };

        public void insert(int doctorid, int patid, int grade, string time, string cont)
        {

            Appraise appraise = new Appraise();
            //通过Session["PatientID"]连接预约信息表得到科室id 医生id 时间段
            appraise.patId = patid;
            appraise.doctorId = doctorid;
            appraise.content = cont;
            appraise.grade = grade;
            appraise.time = time;
            db.Appraise.InsertOnSubmit(appraise);
            db.SubmitChanges();
        }
        public int get_docid(int patid)
        {
            int docid = 0;//通过预约表得到的医生id
                          //通过Session["PatientID"]连接预约信息表得到科室id 医生id 时间段
            var q = db.BookingInfor.Select(e => e.bookId).Max();
            var result = (from r in db.BookingInfor
                          where r.patId == patid && r.bookId == q
                          select r).FirstOrDefault();
            if (result != null)
            {
                docid = (int)result.doctorId;
                return docid;
            }
            else
            {
                return docid;
            }
        }

        public string get_time(int patid)
        {
            var docid = 0;//通过预约表得到的医生id
                          //通过Session["PatientID"]连接预约信息表得到科室id 医生id 时间段
            var q = 1;
            var p = 1;
            if (db.BookingInfor.Count() >= 1)
            {
                p = db.Appraise.Select(e => e.appId).Max();
                q = db.BookingInfor.Select(e => e.bookId).Max();
            }
            var result = (from r in db.BookingInfor
                          where r.patId == patid && r.bookId == q
                          select r).FirstOrDefault();
            if (result != null)
            {
                docid = (int)result.doctorId;
            }
            var result1 = (from g in db.Appraise
                           where g.doctorId == docid && g.appId == p
                           select g).FirstOrDefault();
            if (result1 != null)
            {
                return result1.time;
            }
            else
            {
                return null;
            }
        }
        public string Get_docimage(int patid)
        {

            var q = 1;
            int docid = 0;//通过预约表得到的医生id
            //通过Session["PatientID"]连接预约信息表得到科室id 医生id 时间段
            if (db.BookingInfor.Count() >= 1)
            {
                q = db.BookingInfor.Select(e => e.bookId).Max();
            }
                var result = (from r in db.BookingInfor
                              where r.patId == patid && r.bookId == q
                              select r).FirstOrDefault();
                if (result != null)
                {
                    docid = (int)result.doctorId;
                    // docid = result[result.Count()].doctorId;
                }
                var result1 = (from g in db.Doctor
                               where g.doctorId == docid
                               select g).FirstOrDefault();
                if (result1 != null)
                {
                    var docimage = result1.image;
                    return docimage;
                }
                else return "Prod_Images/null.png";
            
           
        }
        public string Get_docname(int patid)
        {
            var docid = 0;
            var q = 1;
            //通过预约表得到的医生id
            //通过Session["PatientID"]连接预约信息表得到科室id 医生id 时间段
            if (db.BookingInfor.Count() >= 1)
            {
                q = db.BookingInfor.Select(e => e.bookId).Max();
            }
                var result = (from r in db.BookingInfor
                              where r.patId == patid && r.bookId == q
                              select r).FirstOrDefault();
                if (result != null)
                {
                    docid = (int)result.doctorId;
                }
                //通过docid找到医生表中的数据 （医生姓名）
                var result1 = (from g in db.Doctor
                               where g.doctorId == docid
                               select g).FirstOrDefault();
                if (result1 != null)
                {
                    var docname = result1.doctorName;
                    return docname;
                }
                else return "无";
           
        }
        public string get_introduce(int patid)
        {
            var docid = 0;//通过预约表得到的医生id
                          //通过Session["PatientID"]连接预约信息表得到科室id 医生id 时间段
            var q = 1;

            if (db.BookingInfor.Count() >= 1)
            {
                q = db.BookingInfor.Select(e => e.bookId).Max();
            }
                var result = (from r in db.BookingInfor
                              where r.patId == patid && r.bookId == q
                              select r).FirstOrDefault();
                if (result != null)
                {
                    docid = (int)result.doctorId;
                }
                //通过docid找到医生表中的数据 （医生简介）
                var result1 = (from g in db.Doctor
                               where g.doctorId == docid
                               select g).FirstOrDefault();

                if (result1 != null) return result1.introduce;
                else return "您还未预约过任何医生哦，暂无简介";
        }
        public string get_department(int patid)
        {
            var departid = 0;//通过预约表得到的科室id
                             //通过Session["PatientID"]连接预约信息表得到科室id 医生id 时间段
            var q = 1;

            if (db.BookingInfor.Count() >= 1)
            {
                q = db.BookingInfor.Select(e => e.bookId).Max();
            }
                var result = (from r in db.BookingInfor
                              where r.patId == patid && r.bookId == q
                              select r).FirstOrDefault();
                if (result != null)
                {
                    departid = (int)result.departId;
                }
                //通过departid找到科室表中的数据 （科室名）
                var result1 = (from b in db.Department
                               where b.departId == departid
                               select b).FirstOrDefault();
                if (result1 != null)
                {
                    var depname = result1.departName;
                    return depname;
                }
                else return "无";
               
        }
        public string get_content(int patid)
        {
            var docid = 0;//通过预约表得到的医生id
                          //通过Session["PatientID"]连接预约信息表得到科室id 医生id 时间段
            var q = 1;
            var p = 1;
            if (db.BookingInfor.Count() >= 1)
            {
                p = db.Appraise.Select(e => e.appId).Max();
               q = db.BookingInfor.Select(e => e.bookId).Max();
            }
                var result = (from r in db.BookingInfor
                              where r.patId == patid && r.bookId == q
                              select r).FirstOrDefault();
                if (result != null)
                {
                    docid = (int)result.doctorId;
                }
                var result1 = (from g in db.Appraise
                               where g.doctorId == docid && g.appId == p
                               select g).FirstOrDefault();
                if (result1 != null)
                {
                    return result1.content;
                }
                else
                {
                    return null;
                }
           
        }
    }
}
